package com.tongshu.miaosha.controller;

import com.tongshu.miaosha.domain.MiaoshaUser;
import com.tongshu.miaosha.domain.OrderInfo;
import com.tongshu.miaosha.result.CodeMsg;
import com.tongshu.miaosha.result.Result;
import com.tongshu.miaosha.service.GoodsService;
import com.tongshu.miaosha.service.OrderService;
import com.tongshu.miaosha.vo.GoodsVo;
import com.tongshu.miaosha.vo.OrderDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/order")
@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private GoodsService goodsService;

    @GetMapping("/detail")
    @ResponseBody
    public Result<OrderDetailVo> info(Model model, MiaoshaUser user,
                                      @RequestParam("orderId")long orderId) {
        if (user == null)
            return Result.error(CodeMsg.SESSION_ERROR);
        OrderInfo orderInfo = orderService.getOrderById(orderId);
        if (orderInfo == null)
            return Result.error(CodeMsg.ORDER_NOT_EXIST);
        GoodsVo goodsVo = goodsService.getGoodsVoByGoodsId(orderInfo.getGoodsId());
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setGoods(goodsVo);
        orderDetailVo.setOrder(orderInfo);
        return Result.success(orderDetailVo);
    }
}
